package com.sniper.springmvc.scheduler;

import com.gentle.service.comply.TransdataService;
import com.gentle.service.declare.Exception_Exception;
import com.gentle.service.declare.IDataService;
import com.sniper.springmvc.jyzx.JyzxData;
import com.sniper.springmvc.jyzx.JyzxImport;
import com.sniper.springmvc.jyzx.JyzxImportBack;
import com.sniper.springmvc.jyzx.JyzxUtils;
import com.sniper.springmvc.mybatis.service.impl.JyzxReceiveService;
import com.sniper.springmvc.security.SpringContextUtil;
import org.dom4j.DocumentException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class JyzxImportWithScheduled implements Job {

    /**
     * 接收数据关键词
     */
    public static final String type = "jyzxgonggao01";
    public static final String jobName = "jyzx数据导入";

    public static JyzxReceiveService jyzxReceiveService;

    static {
        jyzxReceiveService = SpringContextUtil.getBean(JyzxReceiveService.class);
    }

    /**
     * 秒 时 日 月 周 年
     * 设置每天一点
     */
    protected void init() throws Exception_Exception, NoSuchMethodException, IllegalAccessException
            , DocumentException, InvocationTargetException
            , ExecutionException, InterruptedException {
        TransdataService t = new TransdataService();
        IDataService iDataService = t.getDataServicePort();
        //读取一条数据。确定数据的总记录数
        String dataStr = iDataService.getdata(type, 0, 1);
        JyzxData jyzxData = JyzxUtils.decodeStr(dataStr);
        int total = Integer.valueOf(jyzxData.getTotal());
        //下面是导入本地数据库
        int pageSize = 5000;
        int start = 0;
        int maxThread = 100;

        //分批次读取数据
        int pageCount = (int) Math.ceil(total / pageSize);
        if (total % pageSize > 0) {
            pageCount += 1;
        }
        if (maxThread > pageCount) {
            maxThread = pageCount;
        }
        CountDownLatch latch = new CountDownLatch(maxThread);
        ExecutorService executor = Executors.newFixedThreadPool(maxThread);
        List<Future<JyzxImportBack>> resultList = new ArrayList<>();
        for (int i = 0; i < pageCount; i++) {
            // 为下一页做准备
            JyzxImport jyzxImport = new JyzxImport(jyzxReceiveService, start, pageSize, latch);
            jyzxImport.setiDataService(iDataService);
            start += pageSize;
            Future<JyzxImportBack> future = executor.submit(jyzxImport);
            resultList.add(future);
        }
        executor.shutdown();
        // 获取结果
        for (Future<JyzxImportBack> fs : resultList) {
            //表示数据结果读取
            fs.get();
        }
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            init();
        } catch (Exception_Exception | NoSuchMethodException | IllegalAccessException
                | InvocationTargetException | DocumentException | InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
